package com.ivo.controller.hr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.model.hr.Department;
import com.ivo.model.hr.Employee;
import com.ivo.service.hr.HrService;

@Controller
public class HrController
{
	protected Logger logger = LoggerFactory.getLogger( getClass() );
	private HrService hrService;

	public HrController() {
	}

	@Autowired
	public HrController( HrService hrService ) {
		this.hrService = hrService;
	}

	@RequestMapping( "/hr/getUserList.htm" )
	public ModelAndView execute( HttpServletRequest request, HttpServletResponse response, ModelMap modelMap ) throws ServletException, IOException {

		StringBuffer returnStr = new StringBuffer();

		return new ModelAndView( "ajax", "outString", returnStr.toString() );
	}

	@RequestMapping( "/hr/getEmployeeList.htm" )
	public ModelAndView getEmployeeList( HttpServletRequest request, HttpServletResponse response, ModelMap modelMap ) throws ServletException, IOException {
		StringBuffer returnStr = new StringBuffer();

		String query = request.getParameter( "query" );
		if( query == null || query.equals( "null" ) ) {
			query = "";
		}

		String start = request.getParameter( "start" );
		if( start == null ) start = "0";
		String limit = request.getParameter( "limit" );

		int total = hrService.getResultSize( query );

		if( total > 0 ) {
			returnStr.append( "{'totalProperty':'" + total + "','root':[" );
			int index = Integer.parseInt( start );
			int pageSize = Integer.parseInt( limit );

			List<Employee> result = hrService.fuzzySearchEmployee( index, pageSize, query );

			for( int i = 0; i < result.size(); i++ ) {
				Employee objEmployee = result.get( i );
				String userDept_Id = objEmployee.getDepartment_FK();
				Department department = hrService.getDepartment( userDept_Id );
				String deptName = department.getShortName() + " " + department.getName();

				returnStr.append( "{'employee_ID':'" );
				returnStr.append( objEmployee.getId() );
				returnStr.append( "','employeeName':'" );
				returnStr.append( objEmployee.getName() );
				returnStr.append( "','employeeName_EN':'" );
				returnStr.append( objEmployee.getName_EN() );
				returnStr.append( "','deptName':'" );
				returnStr.append( deptName );
				returnStr.append( "'}" );

				if( i != result.size() - 1 ) {
					returnStr.append( "," );
				}
			}
			returnStr.append( "]}" );

		} else {
			returnStr.append( "{'totalProperty':'0','root':[]}" );
		}
		// System.out.println(returnStr.toString());
		return new ModelAndView( "ajax", "outString", returnStr.toString() );
	}

	@RequestMapping( "/hr/getReCosignerList.htm" )
	public ModelAndView getReCosignerList( HttpServletRequest request, HttpServletResponse response, ModelMap modelMap ) throws ServletException, IOException {
		StringBuffer returnStr = new StringBuffer();

		String reCosignerList = request.getParameter( "reCosignerList" );
		if( reCosignerList.trim().equals( "" ) ) {
			returnStr.append( "{'totalProperty':'0','root':[]}" );
		} else {
			String[] reCosigners = reCosignerList.split( "," );
			ArrayList<Employee> list = new ArrayList<Employee>();
			for( int i = 0; i < reCosigners.length; i++ ) {
				Employee employee = new Employee();
				boolean repeat = false;
				for( int j = 0; j < i; j++ ) {
					if( reCosigners[j].equals( reCosigners[i] ) ) {
						repeat = true;
						break;
					}
				}
				if( repeat ) continue;
				employee = hrService.getEmployee( reCosigners[i] );
				list.add( employee );
			}

			if( list.size() > 0 ) {
				returnStr.append( "{'totalProperty':'" + list.size() + "','root':[" );
				for( int i = 0; i < list.size(); i++ ) {
					Employee objEmployee = list.get( i );
					returnStr.append( "{'employee_ID':'" );
					returnStr.append( objEmployee.getId() );
					returnStr.append( "','employeeName':'" );
					returnStr.append( objEmployee.getName() );
					returnStr.append( " " );
					returnStr.append( objEmployee.getName_EN() );
					returnStr.append( "'}" );

					if( i != list.size() - 1 ) {
						returnStr.append( "," );
					}
				}
				returnStr.append( "]}" );

			} else {
				returnStr.append( "{'totalProperty':'0','root':[]}" );
			}
		}
		// System.out.println(returnStr.toString());
		return new ModelAndView( "ajax", "outString", returnStr.toString() );
	}

}
